
package beaver.game;

import java.io.Serializable;
import java.util.Random;

import org.newdawn.fizzy.Body;

/**
 * This class will hold all relevant information regarding beavers
 * @author Davide Cannucci
 *
 */
@SuppressWarnings("rawtypes")
public class Beaver implements Serializable{

	private static final long serialVersionUID = 1L;
	private int playerID;
	private int id;
	private int hp;
	private Weapon[] weapons;
	private int damageDealt;
	private int killingBlows;
	private int currentWeapon;
	private String name;
	private transient Body body = null;
    private boolean facingR;
	
	public Beaver(int ID,String bname, int player){
		playerID = player;
		id = ID;
		hp = Constants.STARTING_HP;
		weapons = new Weapon[5];
		try{
			weapons[0] = new Weapon(Constants.BAZOOKA);
			weapons[1] = new Weapon(Constants.MINIGUN);
			weapons[2] = new Weapon(Constants.GRENADE);
			weapons[3] = new Weapon(Constants.AIRSTRIKE);
			weapons[4] = new Weapon(Constants.GOLFCLUB);
		}
		catch(Exception e){
			System.err.println("Error constructing weapons set for beaverID " + id );
		}
		damageDealt = 0;
		killingBlows = 0;
		currentWeapon = 0;
		name = bname;
		Random generator = new Random();
		int direction = generator.nextInt(2);
		System.out.println(direction);
		if (direction == 1)
			facingR = true;
		else
			facingR = false;
	}
	public String getName(){
		return name;
	}
	
	public void setBody(Body b){
		body = b;
	}
	
	public Body getBody(){
		return body;
	}
	
	public int getID(){
		return id;
	}
	
	public int getHP(){
		return hp;
	}
	
	public Weapon getCurrentWeapon(){
		return weapons[currentWeapon];
	}
	
	
	/**
	 * @preCondition Beaver must be alive
	 * @param weaponName
	 * @throws Exception
	 */
	public void switchWeapon(String weaponName){
		boolean wasFound = false;
		for(int i = 0;i<weapons.length;i++){
			if(weapons[i].getName().equals(weaponName)){
				currentWeapon = i;
				wasFound = true;
				break;
			}
		}
		
		assert(wasFound == true);

	}
	
	public void cycleWeapon(){
		currentWeapon = ((currentWeapon+1)%weapons.length);
	}
	
	/**
	 * @preCondition Beaver must be alive
	 */
	public void useWeapon(){
		weapons[currentWeapon].use();
	}
	
	/**
	 * @preCondition Beaver must be alive
	 */
	public void reduceHP(int damage){
		if(hp - damage >=0){
			hp = hp - damage;
		}
		else{
			hp=0;
			body.setActive(false);
		}
	}
	
	/**
	 * @preCondition Beaver must be alive
	 */
	public void AddHP(int amount){
		hp = hp + amount;
	}
	
	/**
	 * @preCondition Beaver must be alive
	 */
	public void addKBs(int numberKBs){
		killingBlows = killingBlows + numberKBs;
	}
	
	/**
	 * @preCondition Beaver must be alive
	 */
	public void addDamageDealt(int damage){
		damageDealt = damageDealt + damage;
	}
	
	public int getDamageDealt(){
		return damageDealt;
	}
	
	public void kill(){
		hp = 0;
	}
	

	public int getKBs(){
		return killingBlows;
	}
	
	public boolean isAlive(){
		return hp>0;
	}
	
	public boolean equals(Beaver b){
		boolean same = false;
		if(this.id==b.id && this.name==b.name && this.hp==b.hp){
			same = true;
		}
		return same;
	}
	public int  getPlayerId(){
		return playerID;
	}
	
	public boolean getFacingR(){
		return facingR;
	}
	
	public void setFacingR(boolean direction){
		facingR = direction;
	}
}
